//
//  APSettingViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/9.
//

#import "APSettingViewController.h"
#import "APCopyControl.h"
#import "APSwitchControl.h"
#import "APTitleControl.h"
#import "ApLoginmanager.h"
#import "AppDelegate.h"
#import "APLoginViewController.h"
#import "APDiscoveryDataManager.h"
#import "APUserDataManager.h"
#import "APVideoDataManager.h"
#import "APHomeDataManager.h"
@interface APSettingViewController ()
@property (nonatomic, strong) APCopyControl *idCopyControl;
@property (nonatomic, strong) APSwitchControl *translateControl;
@property (nonatomic, strong) APTitleControl *logoutControl;
@property (nonatomic, strong) APTitleControl *deleteAccountControl;

@end

@implementation APSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNaviBar];
    self.naviBar.titleLabel.text = @"Setting";
    [self initUI];
    [self reloadData];
}


- (void)reloadData{
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    _idCopyControl.titleLabel.text = [NSString stringWithFormat:@"ID: %@",userModel.userId];
    bool isOn = [[NSUserDefaults standardUserDefaults] boolForKey:translateKey];
    [_translateControl.myswitch setOn:isOn];    
}

- (void)translateControlDidClick:(UISwitch *)sender{
    [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:translateKey];
}

- (void)logoutControlDidClick:(APTitleControl *)sender{
    [self.view ShowProgress];
    [APDataManager postJsonWithUrlString:Logout_URL timeoutInterval:0 parameters:nil result:^(NSDictionary * _Nullable res) {
        [[APLoginManager shareInstance] logout];
        [self.view removeProgress];
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        if (![app.window.rootViewController isKindOfClass:[APLoginViewController class]]) {
            app.window.rootViewController  = [[APLoginViewController alloc] init];
        }
    } failBlock:^(NSString * _Nullable errorMsg) {
        [self.view ShowProgressOnlyLabel:errorMsg];
    }];
    [APLogManager statisticWithPage:APPage_logout];

}

- (void)deleteAccountControlDidClick:(APTitleControl *)sender{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Delete account" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"Sure" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self deleteAccount];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]];
    [self presentViewController:alertVC animated:true completion:nil];
}


- (void)deleteAccount {
    [self.view ShowProgress];
    [APDataManager postJsonWithUrlString:DeleteAccount_URL timeoutInterval:0 parameters:nil result:^(NSDictionary * _Nullable res) {
        [[APLoginManager shareInstance] logout];
        [[APUserDataManager shareInstance] clearData];
        [[APDiscoveryDataManager shareInstance] clearData];
        [[APVideoDataManager shareInstance]clearData];
        [[APHomeDataManager shareInstance] clearData];
        [self.view removeProgress];
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        if (![app.window.rootViewController isKindOfClass:[APLoginViewController class]]) {
            app.window.rootViewController  = [[APLoginViewController alloc] init];
        }
    } failBlock:^(NSString * _Nullable errorMsg) {
        [self.view ShowProgressOnlyLabel:errorMsg];
    }];
    [APLogManager statisticWithPage:APPage_deleteAccount];

}

- (void)initUI{
    
    _idCopyControl = [[APCopyControl alloc] init];
    [self.view addSubview:self.idCopyControl];
    [_idCopyControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.naviBar.mas_bottom).offset(12);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.mas_equalTo(48);
    }];
    
    _translateControl = [[APSwitchControl alloc] init];
    [_translateControl.myswitch addTarget:self action:@selector(translateControlDidClick:) forControlEvents:UIControlEventValueChanged];
    _translateControl.titleLabel.text = @"Auto Translate";
    [self.view addSubview:_translateControl];
    [_translateControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_idCopyControl.mas_bottom).offset(16);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.mas_equalTo(48);
    }];
    
    _logoutControl = [[APTitleControl alloc] init];
    [_logoutControl addTarget:self action:@selector(logoutControlDidClick:) forControlEvents:UIControlEventTouchUpInside];
    _logoutControl.titleLabel.text = @"Logout";
    [self.view addSubview:_logoutControl];
    [_logoutControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_translateControl.mas_bottom).offset(16);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.mas_equalTo(48);
    }];
    
    _deleteAccountControl = [[APTitleControl alloc] init];
    [_deleteAccountControl addTarget:self action:@selector(deleteAccountControlDidClick:) forControlEvents:UIControlEventTouchUpInside];
    _deleteAccountControl.titleLabel.text = @"Delete account";
    [self.view addSubview:_deleteAccountControl];
    [_deleteAccountControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_logoutControl.mas_bottom).offset(16);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.mas_equalTo(48);
    }];
}
@end
